package InterfaceDemo;

import java.util.ArrayList;
import java.util.List;

public class OneToMore {
	public static void main(String[] args) {

		Editer editer = new Editer("hero");
		Concrete concrete = new Concrete(editer, "黎明");
		Concrete concrete2 = new Concrete(editer, "刘德华");

//		editer.setMsg("abc");
		 editer.notifyAllObserver();
	}
}

interface Subject {
	void registObserver(Observer observer);

	void removeObserver(Observer observer);

	void notifyAllObserver();
}

class Editer implements Subject {
	private String edi;
	private String msg;

	public Editer(String edi) {
		System.out.println("editer");
		this.edi = edi;
	}

	List<Observer> mList = new ArrayList<Observer>();

	public void registObserver(Observer observer) {

		mList.add(observer);
	}

	public void removeObserver(Observer observer) {

		for (Observer observer1 : mList) {
			if (observer1 != null && observer1 == observer) {
				mList.remove(observer);
			}
		}
	}

	public void notifyAllObserver() {
		for (Observer observer : mList) {
			observer.update(msg + " " + edi + "的新文章AAA");
		}
	}

	/**
	 * 主题更新消息
	 * 
	 * @param msg
	 */
	public void setMsg(String msg) {
		this.msg = msg;

		notifyAllObserver();
	}

}

interface Observer {
	void update(String msg);
}

class Concrete implements Observer {
	String who;

	public Concrete(Subject subject, String who) {
		this.who = who;
		subject.registObserver(this);
	}

	public void update(String msg) {
		System.out.println(who + "----得到消息---" + msg);
	}

}
